<?php

class ImageController extends Controller {

    public function actionResize() {

        $thumbsDir = Yii::app()->basePath . '/..' . Controller::$pageData['_image']['image_dir'] . 'thumbs/';

        $imageFile = Yii::app()->basePath . '/..' . Controller::$pageData['_image']['image_dir'] . Controller::$pageData['_image']['image_file'];

        $ext = explode(".", Controller::$pageData['_image']['image_file']);
        $ext = end($ext);
        $thumbFile = $thumbsDir . str_replace(".", "-", Controller::$pageData['_image']['image_file']) . '_' . Controller::$pageData['_image']['width'] . 'x' . Controller::$pageData['_image']['height'] . "." . $ext;

        $backColor = '0xFFFFFF';

        $noPhoto = false;

        if (!is_file($imageFile)) {
            $imageFile = Yii::app()->basePath . '/../uploads/images/no_photo.jpg';
            $noPhoto = true;
        }

        if (!is_dir($thumbsDir)) {
            mkdir($thumbsDir, 0777);
        }

        $file_stat = getimagesize($imageFile);

        $type = $file_stat['mime'];

        if ((!is_file($thumbFile) && !$noPhoto ) || $noPhoto) {
            switch ($type) {
                case "image/gif": $im = imagecreatefromgif($imageFile);
                    break;
                case "image/jpeg": $im = imagecreatefromjpeg($imageFile);
                    break;
                case "image/png": $im = imagecreatefrompng($imageFile);
                    break;
                case "image/pjpeg": $im = imagecreatefromjpeg($imageFile);
                    break;
            }

            $x = $y = 0;

            list($fileW, $fileH) = $file_stat;

            $ratioW = Controller::$pageData['_image']['width'] / $fileW;
            $ratioH = Controller::$pageData['_image']['height'] / $fileH;
            $ratio = min($ratioH, $ratioW);

            $new_w = $ratio * $fileW;
            $new_h = $ratio * $fileH;

            $x = Controller::$pageData['_image']['width'] == $new_w ? 0 : round((Controller::$pageData['_image']['width'] - $new_w) / 2);
            $y = Controller::$pageData['_image']['height'] == $new_h ? 0 : round((Controller::$pageData['_image']['height'] - $new_h) / 2);

            $im1 = imagecreatetruecolor(Controller::$pageData['_image']['width'], Controller::$pageData['_image']['height']);

            imagefill($im1, 0, 0, $backColor);

            imagecopyresampled($im1, $im, $x, $y, 0, 0, $new_w, $new_h, $fileW, $fileH);

            imageconvolution($im1, [ [ -1, -1, -1], [ -1, 16, -1], [ -1, -1, -1]], 8, 0);

            if (!$noPhoto) {
                imagejpeg($im1, $thumbFile, 100);
                imagedestroy($im);
                imagedestroy($im1);
            }
        }

        if ($noPhoto) {
            
            header("Content-type: {$type}");
            imageJPEG($im1, null, 100);
        } else {
            $data = file_get_contents($thumbFile);
           
            header("Content-type: {$type}");
            header('Content-Length: ' . strlen($data));

            echo $data;
        }
        exit();
    }

}
